import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:flutter_wisave/Tools/NetworkService.dart';
import 'package:flutter_wisave/LoginPage/LoginPage.dart';
import '../../widgets/AppColors.dart';

class PassWordPage extends StatefulWidget {
  const PassWordPage({super.key});

  @override
  State<PassWordPage> createState() => _PassWordPageState();
}

class _PassWordPageState extends State<PassWordPage> {
  final NetworkService _networkService = NetworkService();
  late String _oldPassword = '', _password = '', _password1 = '';
  bool browseOn = false; // Add this line
  bool _isChecked = false; // Add this line to track checkbox state
  get controller => null;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      appBar: AppBar(
        title: const Text(''),
        backgroundColor: Colors.transparent,
        foregroundColor: AppColors.textPrimary,
        flexibleSpace: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/home/home_topbg.png'),
              fit: BoxFit.cover,
            ),
          ),
        ),
      ),
      body: Column(
        children: [
          Container(
            alignment: Alignment.centerLeft,
            margin: const EdgeInsets.only(left: 25, top: 25),
            child: TDText(
              '更换密码',
              textColor: AppColors.textPrimary,
              font: TDTheme.of(context).fontTitleExtraLarge,
            ),
          ),
          Container(
            alignment: Alignment.centerLeft,
            margin: const EdgeInsets.only(left: 25, top: 25, bottom: 10),
            child: TDText(
              '输入原始密码',
              textColor: AppColors.textSecondary,
              font: TDTheme.of(context).fontMarkLarge,
            ),
          ),
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 10),
            child: TDInput(
              type: TDInputType.normal,
              controller: controller,
              obscureText: !browseOn,
              hintText: '请输入您的原始密码',
              hintTextStyle: TextStyle(color: AppColors.hintColor),
              textStyle: TextStyle(color: AppColors.textPrimary),
              backgroundColor: Colors.transparent,
              onChanged: (value) {
                setState(() {
                  _oldPassword = value;
                  print(_oldPassword);
                });
              },
              rightBtn: browseOn
                  ? Icon(
                      TDIcons.browse,
                      color: TDTheme.of(context).fontGyColor3,
                    )
                  : Icon(
                      TDIcons.browse_off,
                      color: TDTheme.of(context).fontGyColor3,
                    ),
              onBtnTap: () {
                setState(() {
                  browseOn = !browseOn;
                });
              },
              needClear: false,
            ),
          ),
          Container(
            alignment: Alignment.centerLeft,
            margin: const EdgeInsets.only(left: 25, top: 25, bottom: 10),
            child: TDText(
              '输入新密码',
              textColor: AppColors.textSecondary,
              font: TDTheme.of(context).fontMarkLarge,
            ),
          ),
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 10),
            child: TDInput(
              type: TDInputType.normal,
              controller: controller,
              obscureText: !browseOn,
              hintText: '请输入您的密码',
              textStyle: TextStyle(color: AppColors.textPrimary),
              hintTextStyle: TextStyle(color: AppColors.hintColor),
              backgroundColor: Colors.transparent,
              onChanged: (value) {
                setState(() {
                  _password = value;
                  print(_password);
                });
              },
              rightBtn: browseOn
                  ? Icon(
                      TDIcons.browse,
                      color: TDTheme.of(context).fontGyColor3,
                    )
                  : Icon(
                      TDIcons.browse_off,
                      color: TDTheme.of(context).fontGyColor3,
                    ),
              onBtnTap: () {
                setState(() {
                  browseOn = !browseOn;
                });
              },
              needClear: false,
            ),
          ),
          Container(
            alignment: Alignment.centerLeft,
            margin: const EdgeInsets.only(left: 25, top: 25, bottom: 10),
            child: TDText(
              '再次输入新密码',
              textColor: AppColors.textSecondary,
              font: TDTheme.of(context).fontMarkLarge,
            ),
          ),
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 10),
            child: TDInput(
              type: TDInputType.normal,
              controller: controller,
              obscureText: !browseOn,
              hintText: '请输入您的密码',
              textStyle: TextStyle(color: AppColors.textPrimary),
              hintTextStyle: TextStyle(color: AppColors.hintColor),
              backgroundColor: Colors.transparent,
              onChanged: (value) {
                setState(() {
                  _password1 = value;
                  print(_password1);
                });
              },
              rightBtn: browseOn
                  ? Icon(
                      TDIcons.browse,
                      color: TDTheme.of(context).fontGyColor3,
                    )
                  : Icon(
                      TDIcons.browse_off,
                      color: TDTheme.of(context).fontGyColor3,
                    ),
              onBtnTap: () {
                setState(() {
                  browseOn = !browseOn;
                });
              },
              needClear: false,
            ),
          ),
          Container(
            margin: const EdgeInsets.only(top: 30),
            child: TextButton(
                onPressed: () async {
                  if (_oldPassword.isEmpty) {
                    TDToast.showText('请输入您的原始密码', context: context);
                    return;
                  }
                  if (_password.isEmpty) {
                    TDToast.showText('请输入您的密码', context: context);
                    return;
                  }
                  if (_password1.isEmpty) {
                    TDToast.showText('请再次输入您的密码', context: context);
                    return;
                  }
                  if (_password1 != _password) {
                    TDToast.showText('两次密码不一致', context: context);
                    return;
                  }
                  try {
                    final response = await _networkService.post(
                      '/system/user/resetUserPwd',
                      data: {
                        'firstPassword': _oldPassword, //'admin',_email,
                        'secondPassword': _password, //'ldkj123.', _password
                      },
                    );
                    TDToast.showSuccess('修改成功',
                        direction: IconTextDirection.vertical,
                        context: context);

                    Navigator.pop(context);
                  } catch (e) {
                    print('Error: $e');
                  }
                },
                style: TextButton.styleFrom(
                  padding: EdgeInsets.zero,
                  backgroundColor: Colors.transparent,
                  shadowColor: Colors.transparent,
                ),
                child: Container(
                  width: MediaQuery.of(context).size.width - 40,
                  height: 60,
                  decoration: BoxDecoration(
                    image: const DecorationImage(
                      image: AssetImage(
                          'assets/images/login/login_bg.png'), // Replace with your image asset
                      fit: BoxFit.fill,
                    ),
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: const Center(
                    child: Text(
                      '完成',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 18,
                      ),
                    ),
                  ),
                )),
          ),
        ],
      ),
    );
  }
}
